package com.tulei.system.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.tulei.common.utils.websocket.Message;
import com.tulei.common.utils.websocket.WebSocketServer;
import com.tulei.system.Entity.SysUser;
import com.tulei.system.service.MessageService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author TuLei
 * @Date 2024-06-10 17:24
 **/
@Service
@RequiredArgsConstructor
public class MessageServiceImpl implements MessageService {


    private final WebSocketServer webSocketServer;
    @Override
    public void sendMsg(Message message) {
        SysUser info = (SysUser) StpUtil.getSession().get("user");
        message.setSenderId(Long.valueOf(info.getUserId()));
        List<Long> onLineUserIds = webSocketServer.getOnLineUserIds();
        if (CollectionUtils.isEmpty(onLineUserIds)) {
            return;
        }
        Long receiverId = message.getReceiverId();
        message.setReceiverId(receiverId);
        webSocketServer.sendOneMessage(message);
    }
}
